<?php
namespace Pay\Controller;

class ApiController extends CommonController {
	public function GameList(){
		$letters = I('post.letters');
		preg_match('/^[a-z]+$/i', $letters, $matches);
		$rs = array();
		if( $letters != '' && $matches ){
			$db = zyt_data_init();
			$list = $db->getGameList();
			$letter_arr = letter_slice($letters);
			foreach( $list as $v ){
				if( $v['game_is_recharge'] == 1 && in_array($v['game_name_initials'], $letter_arr)){
					$rs[] = $v;
				}
			}
		}
		$this->ajaxReturn($rs);
	}

	public function GameServer(){
		$gid = trim(I('post.gid'));
		$rs = array();
		if( (int)$gid > 0 ){
			$db = zyt_data_init();
			$currency = sliceGameRate($db->getGameInfo($gid)[0]['game_ratio_desc']);
			$rs = $db->getGameServer($gid);
			$rs['currency'] = $currency;
		}
		$this->ajaxReturn($rs);
	}

	//验证支付密码
	public function ChkPayPwd(){
		$pwd = trim(I('post.v'));
		$result = array('code'=>0, 'msg'=>'请先登录');
		if( ! $pwd || ! $this->user ){
			$this->ajaxReturn($result);
		}
		$db = zyt_data_init();
		$data = $this->_chkPayPwd($pwd, $db);
		$result = false === $data ? array('code'=>0, 'msg'=>'支付密码错误') : array('code'=>1, 'msg'=>'正确');
		$this->ajaxReturn($result);
	}
	//支付密码是否为空
	public function ChkPayPwdEmpty(){
		$db = zyt_data_init();
		$id=$_SESSION['userinfo']['user_id'];
		if($id!=''){
			$data = $this->_chkPayPwdEmpty($db,$id);
			if($data['user_paypassword']==''){$result=2;}
		}
		$this->ajaxReturn($result);
	}

	//验证支付信息
	public function CheckPay(){
		$post  = I('post.');
		$pwd   = trim($post['p']);
		$code  = trim($post['c']);

		$result = array('code'=>0, 'msg'=>'');

		if( empty($pwd) || empty($code) ) {
			$msg = '支付密码和验证码不能为空.';
			$result['msg'] = $msg;
			$this->ajaxReturn($result);
		}

		$db = zyt_data_init();
		//验证支付密码
		$chk_ppw = $this->_chkPayPwd($pwd, $db);
		if( $chk_ppw === false ){
			$msg = '支付密码错误.';
			$result['msg'] = $msg;
			$this->ajaxReturn($result);
		}

		//验证手机验证码
		$chk_psc = $this->_chkVerifyCode($code, $db);
		if( $chk_psc === false ){
			$msg = '手机验证码错误或验证码已失效.';
			$result['msg'] = $msg;
			$this->ajaxReturn($result);
		}

		//验证通过，记录SESSION
		session('SECURITY_PASS', null);
		session('SECURITY_PASS', md5($chk_ppw.$chk_psc));
		//返回成功消息
		$result['code'] = 1;
		$result['msg']  = '验证通过';
		$this->ajaxReturn($result);
	}


	//发送手机验证码
	public function SendCode(){
		if( ! $this->user )                          $this->ajaxReturn('您还没有登录'); //未登录
		if( empty(trim($this->user['user_phone'])) ) $this->ajaxReturn('您还没有通过手机号码验证'); //未通过手机验证

		$db = zyt_data_init();
		$data = $db->table('send_code')->field('code_send_time')->where("code_bind_number = '{$this->user['user_phone']}'")->order('code_id desc')->find();
		$last = (int)$data['code_send_time'];

		$C = C('SMS');
		$sms_code  = $this->_getCode();
		if( ! C('APP_DEBUG') ){
			if( time() - $last <= $C['GAP'] )   $this->ajaxReturn('发送过于频繁，请稍候再发送');    //发送过于频繁
			$post_data = array(
				'account'  => $C['ACCOUNT'],
				'password' => $C['PASSWORD'],
				'mobile'   => $this->user['user_phone'],
				'content'  => '您的认证码是：' . $sms_code . '。请不要把认证码泄露给其他人。如非本人操作，可不用理会！',
			);
			$result = xml_to_array(curl_post($C['URL'], $post_data));
			if( $result['SubmitResult']['code'] == 2 ){
				//发送成功，写入数据库
				$db->saveVerifyCode($this->user['user_phone'], $sms_code, 2);
				//返回成功消息
				$this->ajaxReturn('认证码已成功发送到您的手机上，请注意查收!');
			}else{
				$this->ajaxReturn('手机认证码发送失败，请稍候再试。'); //返回发送失败
			}
		}else{
			$db = zyt_data_init();
			$db->saveVerifyCode($this->user['user_phone'], $sms_code, 2);
			$this->ajaxReturn($sms_code);
		}
	}


	//生成手机验证码
	private function _getCode(){
		$len  = 6;   //长度
		$code = '';
		for($i = 0; $i < $len; $i ++){
			$code .=  rand(0, 9);
		}
		return $code;
	}
}